package org.ph.share.lock.test.thread2;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 线程状态
 * wait -> notify
 * @author Hush.
 * @since 2023/1/18 19:47
 */
@Slf4j
public class Thread7 {
    private static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("t1,get lock");
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("t2,get lock");
            }
        }, "t2");
        t2.start();

        TimeUnit.SECONDS.sleep(1);
        synchronized (lock) {
            lock.notifyAll();
        }
    }
}
